<?php


namespace app\superadmin\controller;


use function Couchbase\defaultDecoder;
use think\Db;
use think\facade\Request;

class CompanyUser extends LoggingBase
{

    #region 界面
    public function Index()
    {

    }

    public function CompanyAdd()
    {
        $Company = array(
            'ID' => '0',
            'Name' => '',
            'Number' => '',
            'Type' => 0,
            'Scale' => 0,
            'Model' => '0',
            'Capital' => '',
            'TimeRegist' => '',
            'Address' => '',
            'Scope' => ''
        );
        $this->assign('HandleName', '新增企业');
        $this->assign('HandleType', 'Add');
        $this->assign('Company', $Company);
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('新增企业', 'CompanyAdd', 'company_add', $Css);
    }

    public function CompanyEdit()
    {
        $CompanyID = Request::has('CompanyID') ? intval(Request::param('CompanyID')) : 0;
        if (!$CompanyID) {
            $this->error('参数错误！');
            return;
        }
        $Company = Db::table('company')->where("ID='$CompanyID' AND IsDeleted='0'")->find();
        if ($Company == null) {
            $this->error('没有该企业记录');
            return;
        }
        $Users = Db::table('user')->where('CompanyID', $CompanyID)->select();
        $this->assign('HandleName', '编辑企业');
        $this->assign('HandleType', 'Edit');
        $this->assign('Company', $Company);
        $this->assign('Users', $Users);
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('编辑企业', 'CompanyEdit', 'company_add', $Css);
    }

    public function CompanyList()
    {
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('企业列表', 'CompanyList', 'company_list', $Css);
    }

    public function UserAdd()
    {
        $User = array(
            'ID' => 0,
            'Name' => '',
            'Nick' => '',
            'WeChat' => '',
            'QQ' => '',
            'Phone' => '',
            'Email' => '',
            'Sex' => 0,
            'Remark' => '',
            'CompanyID' => 0,
            'DepartmentID' => 0,
            'PostID' => 0
        );
        $Companys = Db::table('company')->where("UserID='0' AND IsDeleted='0'")->select();

        $this->assign('HandleName', '新增用户');
        $this->assign('HandleType', 'Add');
        $this->assign('User', $User);
        $this->assign('Companys', $Companys);
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('新增用户', 'UserAdd', 'user_add', $Css);
    }

    public function UserEdit()
    {
        $UserID = Request::has('UserID') ? intval(Request::param('UserID')) : 0;
        if (!$UserID) {
            $this->error('参数错误！');
            return;
        }
        $User = Db::table('user')->where("ID='$UserID' AND IsDeleted='0'")->find();
        if ($User == null) {
            $this->error('没有该用戶记录');
            return;
        }
        $Companys = Db::table('company')->where("IsDeleted='0'")->select();

        $this->assign('HandleName', '编辑用户');
        $this->assign('HandleType', 'Edit');
        $this->assign('User', $User);
        $this->assign('Companys', $Companys);
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('编辑用户', 'UserEdit', 'user_add', $Css);
    }

    public function UserList()
    {
        $Css = array(
            '<link href="/layui/css/layui.css" rel="stylesheet"/>'
        );
        return $this->mb_Display('用户列表', 'UserList', 'user_list', $Css);
    }

    #endregion

    #region 接口


    #region 企业

    /**
     * 提交企業信息
     */
    public function mb_SubmitCompany()
    {
        $HandleType = Request::has('HandleType') ? Request::param('HandleType') : '';
        $ID = Request::has('ID') ? Request::param('ID') : '';
        $Name = Request::has('Name') ? Request::param('Name') : '';
        $Number = Request::has('Number') ? Request::param('Number') : '';
        $Type = Request::has('Type') ? Request::param('Type') : '';
        $Scale = Request::has('Scale') ? Request::param('Scale') : '';
        $Model = Request::has('Model') ? Request::param('Model') : '';
        $Capital = Request::has('Capital') ? Request::param('Capital') : '';
        $TimeRegist = Request::has('TimeRegist') ? Request::param('TimeRegist') : '';
        $Address = Request::has('Address') ? Request::param('Address') : '';
        $Scope = Request::has('Scope') ? Request::param('Scope') : '';
        $UserID = Request::has('UserID') ? Request::param('UserID') : '';

        $Message = '';
        Db::startTrans();
        try {
            if ($HandleType == 'Add') {
                $Data = array(
                    'UserID'=>$UserID,
                    'Name' => $Name,
                    'Number' => $Number,
                    'Type' => $Type,
                    'Scale' => $Scale,
                    'Model' => $Model,
                    'Capital' => $Capital,
                    'TimeRegist' => $TimeRegist,
                    'Address' => $Address,
                    'Scope' => $Scope
                );
                $CompanyID = Db::table('company')->insertGetId($Data);

                $Data = array(
                    'CompanyID' => $CompanyID,
                    'PID' => 0,
                    'Name' => '总裁办',
                    'Describe' => ''
                );
                $DepartmentID = Db::table('company_department')->insertGetId($Data);

                $Data = array(
                    'DepartmentID' => $DepartmentID,
                    'PID' => 0,
                    'Name' => '总经理',
                    'Describe' => '',
                );
                $PostID = Db::table('company_department_post')->insertGetId($Data);

                $Message = '新增成功';
            } else if ($HandleType == 'Edit') {
                $Data = array(
                    'UserID'=>$UserID,
                    'Name' => $Name,
                    'Number' => $Number,
                    'Type' => $Type,
                    'Scale' => $Scale,
                    'Model' => $Model,
                    'Capital' => $Capital,
                    'TimeRegist' => $TimeRegist,
                    'Address' => $Address,
                    'Scope' => $Scope
                );
                Db::table('company')->where('ID', $ID)->update($Data);
                $Message = '修改成功';
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, $Message);
    }

    /**
     *
     */
    public function mb_CompanyList()
    {
        $Page = Request::has('page') ? intval(Request::param('page')) : 1;
        $Limit = Request::has('limit') ? intval(Request::param('limit')) : 10;
        $Count = Db::table('company')->where('IsDeleted', '0')->count();
        $Data = Db::table('company')->where('IsDeleted', '0')->limit($Limit)->page($Page)->select();
        foreach ($Data as $Key => $Val) {
            $Data[$Key]['TypeName'] = $this->mb_GetCompanyTypeName($Val['Type']);
            $Data[$Key]['ScaleName'] = $this->mb_GetCompanyScaleName($Val['Scale']);
            $Data[$Key]['ModelName'] = $this->mb_GetCompanyModelName($Val['Model']);
            $Data[$Key]['Capital'] = $Val['Capital'] . '万人民币';
        }
        $Send = array(
            'code' => 0,
            'msg' => '',
            'count' => $Count,
            'data' => $Data
        );
        return $Send;
    }
    #endregion

    #region 用户

    public function mb_SubmitUser()
    {
        $HandleType = Request::has('HandleType') ? Request::param('HandleType') : '';
        $ID = Request::has('ID') ? Request::param('ID') : '';
        $Name = Request::has('Name') ? Request::param('Name') : '';
        $Nick = Request::has('Nick') ? Request::param('Nick') : '';
        $BasePass = Request::has('Password') ? Request::param('Password') : '';
        $WeChat = Request::has('WeChat') ? Request::param('WeChat') : '';
        $QQ = Request::has('QQ') ? Request::param('QQ') : '';
        $Phone = Request::has('Phone') ? Request::param('Phone') : '';
        $Email = Request::has('Email') ? Request::param('Email') : '';
        $Sex = Request::has('Sex') ? Request::param('Sex') : 0;
        $Remark = Request::has('Remark') ? Request::param('Remark') : '';

        $CompanyID = Request::has('CompanyID') ? Request::param('CompanyID') : 0;
        $DepartmentID = Request::has('DepartmentID') ? Request::param('DepartmentID') : 0;
        $PostID = Request::has('PostID') ? Request::param('PostID') : 0;

        $SuperiorID = Request::has('SuperiorID') ? Request::param('SuperiorID') : 0;

        $Message = '';
        Db::startTrans();
        try {
            $Data = array(
                'Name' => $Name,
                'Nick' => $Nick,
                'BasePass' => $BasePass,
                'EncPass' => md5($BasePass),
                'WeChat' => $WeChat,
                'QQ' => $QQ,
                'Phone' => $Phone,
                'Email' => $Email,
                'Sex' => $Sex,
                'Remark' => $Remark,
                'Level' => 2,
                'CompanyID' => $CompanyID,
                'DepartmentID' => $DepartmentID,
                'PostID' => $PostID,
                'SuperiorID' => $SuperiorID
            );
            if ($HandleType == 'Add') {
                Db::table('user')->insertGetId($Data);
                $Message = '新增成功';
            } else if ($HandleType == 'Edit') {
                Db::table('user')->where('ID', $ID)->update($Data);
                $Message = '修改成功';
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, $Message);
    }

    private function mb_GetCompanyTypeName($Index)
    {
        //企業類型（0：請選擇，1：股份有限公司，2：有限公司，3：合夥企業，4：個人獨資企業，5：個體工商戶，6：其他）
        $Name = '';
        switch ($Index) {
            case 1:
                $Name = '股份有限公司';
                break;
            case 2:
                $Name = '有限公司';
                break;
            case 3:
                $Name = '合伙企业';
                break;
            case 4:
                $Name = '个人独资企业';
                break;
            case 5:
                $Name = '个体工商户';
                break;
            case 6:
                $Name = '其他';
                break;
            default:
                break;
        }
        return $Name;
    }

    private function mb_GetCompanyScaleName($Index)
    {
        //規模（0：請選擇，1：1-50人，2：51-100人，3：101-300人，4：301-500人，5：501人以上）
        $Name = '';
        switch ($Index) {
            case 1:
                $Name = '1-50人';
                break;
            case 2:
                $Name = '51-100人';
                break;
            case 3:
                $Name = '101-300人';
                break;
            case 4:
                $Name = '301-500人';
                break;
            case 5:
                $Name = '501人以上';
                break;
            default:
                break;
        }
        return $Name;
    }

    private function mb_GetCompanyModelName($Index)
    {
        //經營模式（0：請選擇，1：生產型，2：供應型，3：加工型，4：代理型）
        $Name = '';
        switch ($Index) {
            case 1:
                $Name = '生产型';
                break;
            case 2:
                $Name = '供应型';
                break;
            case 3:
                $Name = '加工型';
                break;
            case 4:
                $Name = '代理型';
                break;
            default:
                break;
        }
        return $Name;
    }

    public function mb_GetDepartments()
    {
        $CompanyID = Request::has('CompanyID') ? Request::param('CompanyID') : 0;
        if (!$CompanyID) {
            mb_OutMessage(ErrCode_Toast, '参数有误！');
        }
        $Data = Db::table('company_department')->where("CompanyID='$CompanyID' AND IsDeleted='0'")->select();
        mb_OutMessage(ErrCode_Normal, '', $Data);
    }

    public function mb_GetPosts()
    {
        $DepartmentID = Request::has('DepartmentID') ? Request::param('DepartmentID') : 0;
        if (!$DepartmentID) {
            mb_OutMessage(ErrCode_Toast, '参数有误！');
        }
        $Data = Db::table('company_department_post')->where("DepartmentID='$DepartmentID' AND IsDeleted='0'")->select();
        mb_OutMessage(ErrCode_Normal, '', $Data);
    }

    public function mb_GetCompanyUsers()
    {
        $CompanyID = Request::has('CompanyID') ? Request::param('CompanyID') : 0;
        if (!$CompanyID) {
            mb_OutMessage(ErrCode_Toast, '参数有误！');
        }
        $Data = Db::table('user')->where("CompanyID='$CompanyID' AND IsDeleted='0'")->select();
        mb_OutMessage(ErrCode_Normal, '', $Data);
    }

    public function mb_UserList()
    {
        $Page = Request::has('page') ? Request::param('page') : 1;
        $Limit = Request::has('limit') ? Request::param('limit') : 10;
        $Count = Db::table('user')->where('IsDeleted', 0)->count();
//        $Data = Db::table('user')->where('IsDeleted', 0)->order('ID', 'DESC')->limit($Limit)->page($Page)->select();
        $Data = Db::query("SELECT u.*, c.Name AS CompanyName FROM user AS u LEFT JOIN company AS c ON c.ID = u.CompanyID WHERE u.IsDeleted='0';");
        foreach ($Data as $Key => $Val) {
            $SexName = '';
            if ($Val['Sex'] == 1) $SexName = '男';
            else if ($Val['Sex'] == 2) $SexName = '女';
            $Data[$Key]['SexName'] = $SexName;
        }
        mb_OutMessage(ErrCode_Normal, '', $Data, $Count);

    }
    #endregion

    #endregion

}